package com.hs.mobile.dao.notifications;

import java.util.Date;
import java.util.List;

import com.hs.mobile.model.Device;
import com.hs.mobile.model.Notifications;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Query;

public class NotificationsDAOImpl implements NotificationsDAO {
	
	SessionFactory sessionFactory;
	
	//Setter
	/**
	 * 
	 * @return sessionFactory
	 */
	public SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}
	//setter
	/**
	 * 
	 * @param sessionFactory
	 */
	public void setSessionFactory( SessionFactory sessionFactory )
	{
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List getListAll() {
		// TODO Auto-generated method stub
		System.out.println("__ Get list notifications __ ");
		Session session = getSessionFactory().openSession();
		List<Notifications> listNotifications = null;
		try {
			Criteria cr = session.createCriteria(Notifications.class);
			listNotifications = cr.list();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __ ");
		} finally {
			session.close();
		}		
		return listNotifications;
	}

	@Override
	public Object getById(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get Notification __");
		Session session = getSessionFactory().openSession();
		Notifications notification = null;
		try {
			notification = (Notifications)session.get(Notifications.class, id);			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return notification;
	}

	@Override
	public void add(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Save Notification __ ");
		Session session = getSessionFactory().openSession();
		Notifications notification = ( Notifications ) obj;
		try {
			session.beginTransaction();
			session.save(notification);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Save Error __");
		} finally {
			session.close();
		}		
	}

	@Override
	public void delete(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Delete __");
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(obj);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("_ Delete error _ ");
		} finally { 
			session.close();
		}
	}

	@Override
	public void update(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Update __");
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.update(obj);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Update __");
		} finally {
			session.close();
		}
	}

	@Override
	public void updateStatus(long id, int status, Date modified) {
		// TODO Auto-generated method stub
		System.out.println("__ Update __");
		Session session = getSessionFactory().openSession();
		Notifications notifications  = ( Notifications ) getById(id);
		notifications.setUpdateStatus(status);
		try {
			session.beginTransaction();
			session.update(notifications);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Update __");
		} finally {
			session.close();
		}
	}

	@Override
	public int getCountPush() {
		// TODO Auto-generated method stub
		System.out.println("__ Get Count Push __ ");
		Session session = getSessionFactory().openSession();
		int count = 0;
		try {
			String sql = " Select COUNT(*) from NOTIFICATIONS ";
			count = session.createSQLQuery(sql).list().size();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Count Error __ ");
		} finally {
			session.close();
		}
		return count;
	}

	@Override
	public Date getLastPush() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Notifications findByNotificationId(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get Notification __");
		Session session = getSessionFactory().openSession();
		Notifications notification = null;
		try {
			notification = (Notifications)session.get(Notifications.class, id);			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return notification;
	}

	@Override
	public List<Notifications> getNotificationsByDeviceId(long deviceId) {
		// TODO Auto-generated method stub
		System.out.println("__ Get list notifications __ ");
		Session session = getSessionFactory().openSession();
		List<Notifications> listNotifications = null;
		try {
			String sql = "Select * from NOTIFICATIONS where ID_DEVICE =  " + deviceId;
			SQLQuery query = session.createSQLQuery(sql);
	        query.addEntity(Device.class);
	        listNotifications = query.list();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __ ");
		} finally {
			session.close();
		}		
		return listNotifications;
	}

	@Override
	public List<Notifications> getNotificationsByServiceId(long serviceId) {
		// TODO Auto-generated method stub
		System.out.println("__ Get list notifications __ ");
		Session session = getSessionFactory().openSession();
		List<Notifications> listNotifications = null;
		try {
			String sql = "Select * from NOTIFICATIONS where ID_SERVICE =  " + serviceId;
			SQLQuery query = session.createSQLQuery(sql);
	        query.addEntity(Device.class);
	        listNotifications = query.list();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __ ");
		} finally {
			session.close();
		}		
		return listNotifications;
	}
}
